# -*- coding: utf-8 -*-
import urllib, urllib2, cookielib,re
import os,sys,os.path, time
import Queue, threading

q = Queue.Queue()
q_photo = Queue.Queue()
THREAD_NUMBER = 5
PHOTOS_THREAD = 30
USER_THREAD = 5

cookiejar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
exheaders = [("User-Agent","Mozilla/4.0 (compatible; MSIE 7.1; Windows NT 5.1; SV1)"),]
opener.addheaders=exheaders

def login(email,password):
    login_url = 'http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=http%3A%2F%2Fwww.renren.com%2FHome.do'
    response = opener.open(login_url, urllib.urlencode({
        'email':email,
        'password':password,
        'autoLogin':'true',
        'domain':'renren.com',
        'isplogin':'true',
        'submit':'%E7%99%BB%E5%BD%95'})).read()

def nextPage(url):
    curpage = int(re.search('curpage=(\d+)',url).groups()[0])
    nexturl = url[:url.rindex('=')+1]+ str(curpage+1)
    return nexturl
def encode(string):
    #print string
    codetype = sys.getfilesystemencoding()
    string = string.decode('UTF-8').encode(codetype)
    return string
class PhotoLinkThread(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.dirs = ''
    def run(self):
        while True:
            job = self.queue.get()
            self.getLinks(job)
            self.queue.task_done()
    def getLinks(self,job):
        getPhotoLinks(job)
        
def getPhotoLinks(link):
    time.sleep(1)
    page2 = opener.open(link).read()
    opener.close()
    album_name = re.search("<h1>(.*?)</h1>",page2).groups()[0].decode('utf-8')
    album_name = parse(album_name)
    photo_url = re.findall("<img lazy-src=\"(.*?)\"",page2)
    if len(photo_url) == 20:
        q.put(nextPage(link))
    if not os.path.exists(album_name):
        os.mkdir(album_name)
    for p in photo_url:
        #print [album_name,photo_url]
        q_photo.put([album_name,p])
                  
class downloadThread(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.dirs = ''
    def run(self):
        while True:
            job = self.queue.get()
            self.ff(job)
            self.queue.task_done()
    def ff(self,job):
        urllib.urlretrieve(job[1], job[0]+'\\'+job[1][job[1].rindex('/')+1:])

##class UserThread(threading.Thread):
##    def __init__(self,queue):
##        threading.Thread.__init__(self)
##        self.queue = queue
##    def run(self):
##        while True:
##            job = self.queue.get()
##            self.ff(job)
##            self.queue.task_done()
##    def ff(self,job):
##        urllib.urlretrieve(job[1], job[0]+'\\'+job[1][job[1].rindex('/')+1:])
def parse(string):
    for each in ['/','\\','|',':','*','?','<','>','"']:
        string = string.replace(each,'')
    return string.strip()
def init(e):    
    profile_url = "http://www.renren.com/profile.do?id=%s" % e
    infor = re.search("<title>(.*?)</title>",opener.open(profile_url).read(),re.DOTALL).groups()[0]
    opener.close()
    infor = parse(infor).replace('\n','').replace(' ','')#.decode('utf-8')
    name = infor[infor.index('-')+1:infor.index('(')]
    school = infor[infor.index('(')+1:-1]
    user_dir = encode('%s_%s_%s' % (e,name,school))
    try:
        os.mkdir(user_dir)
    except:
        pass
    os.chdir(user_dir)
    print encode('正在下载 %s 的照片......\n' % name)
    photos = "http://photo.renren.com/photo/%s/album/relatives/?curpage=0" % e
    catagories = []
    while True:
        tmp = re.findall("<span class=\"infor\">.*?href=\"(.*?)\">(.*?)</a>",opener.open(photos).read())
        opener.close()
        catagories.extend(tmp)
        if len(tmp) < 20:
            break
        else:    
            photos = nextPage(photos)
    print len(catagories)
    THREAD_NUMBER = len(catagories)
    for each in catagories:
        q.put(each[0]+'?curpage=0')  
def main():
    email = raw_input(encode("登陆账号:"))
    password = raw_input(encode("登陆密码:"))
    login(email,password)
    choice = raw_input(encode("""
1. 下载指定ID的所有相册
2. 下载指定链接相册
"""))
    if choice == '1':
        user_id = raw_input(encode("输入下载账号ID(多个ID请用','隔开):"))
        user_id = user_id.split(',')
        cwd = os.getcwd()
        for each_id in user_id:
            os.chdir(cwd)
            init(each_id)
            for x in range(THREAD_NUMBER):
                t = PhotoLinkThread(q)
                t.setDaemon(True)
                t.start()
            for x in range(PHOTOS_THREAD):
                t = downloadThread(q_photo)
                t.setDaemon(True)
                t.start()
            q.join()
            q_photo.join()
    else:
        link = raw_input(encode("请输入相册链接：\n"))
        getPhotoLinks(link.strip())
        for x in range(PHOTOS_THREAD):
            t = downloadThread(q_photo)
            t.setDaemon(True)
            t.start()
        q_photo.join()
        

    print encode('下载完成！')
        
if __name__ == '__main__':
    main()
